package frame2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;

/**
 * Created on Mar 30, 2007
 * @author Anand
 */
public class Sample033007 extends JFrame implements ActionListener{

	Container container;
	JMenuBar menubar;
	JMenu menuFile;
	JMenuItem open;
	JMenuItem save;
	JMenuItem print;
	JMenuItem pageSetup;
	JMenuItem exit;
	
	JTabbedPane mainTab;
	JTabbedPane leftTab;
	JTabbedPane rightTab;
	
	JPanel mainPanel;
	JPanel comboPanel;
	JPanel chkbxPanel;
	JPanel tablePanel;
	JPanel listPanel;
	
	JComboBox combo;
	JTextArea txtarea;
	JTable table;
	JList list;
	JLabel label;
	JCheckBox chkbox1;
	JCheckBox chkbox2;
	JCheckBox chkbox3;
	JCheckBox chkbox4;
	
	public Sample033007(){
		super(":: Sample Demo of File Dialog, Print Dialog, Page Setup & Panel Nesting ::");
		container = getContentPane();
		menubar = new JMenuBar();
				
		mainPanel = new JPanel(new GridLayout());
		comboPanel = new JPanel(new BorderLayout());
		chkbxPanel = new JPanel(new GridLayout(5,0));
		tablePanel = new JPanel(new FlowLayout());
		listPanel = new JPanel(new GridLayout());
		
		mainTab = new JTabbedPane();
		leftTab = new JTabbedPane();
		rightTab = new JTabbedPane();
						
		int vsb = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
		int hsb = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
		
		Object []cmbListData = {"Red","Blue","Green","Orange","White","Black","Gray","Yellow","Brown"};
		Object []tableHeader = {"Color","Value"};
		Object [][] data = {{"Red","FF0000"},{"Green","00FF00"},{"Blue","0000FF"}};
		
		combo = new JComboBox(cmbListData);
		list = new JList(cmbListData);
		table = new JTable(data,tableHeader);
		
		txtarea = new JTextArea(10,10);
		label = new JLabel("Select your Option(s) :- ");
		label.setFont(new Font("Courier",Font.PLAIN,12));		

		chkbox1 = new JCheckBox("Option A");
		chkbox2 = new JCheckBox("Option B");
		chkbox3 = new JCheckBox("Option C");
		chkbox4 = new JCheckBox("Option D");
				
		comboPanel.add(combo,"North");
		comboPanel.add(new JScrollPane(txtarea,vsb,hsb),"Center");
		
		chkbxPanel.add(label);
		chkbxPanel.add(chkbox1);
		chkbxPanel.add(chkbox2);
		chkbxPanel.add(chkbox3);
		chkbxPanel.add(chkbox4);
		
		tablePanel.add(new JScrollPane(table, vsb,hsb));
		listPanel.add(new JScrollPane(list, vsb,hsb));
	
		mainPanel.add(leftTab);
		mainPanel.add(rightTab);
		
		leftTab.addTab("Combo Box",comboPanel);
		leftTab.addTab("Check Box",chkbxPanel);
		rightTab.addTab("Table", tablePanel);
		rightTab.addTab("List", listPanel);		
		
		mainTab.addTab("Main Tab", mainPanel);
		
		container.add(mainTab);
		
		menuFile = new JMenu("File");
		menuFile.setMnemonic('F');
		
		open = new JMenuItem("Open");
		save = new JMenuItem("Save");
		print = new JMenuItem("Print");
		pageSetup = new JMenuItem("Page SetUp");
		exit = new JMenuItem("Exit");
		
		open.addActionListener(this);
		save.addActionListener(this);
		print.addActionListener(this);
		pageSetup.addActionListener(this);
		exit.addActionListener(this);
		
		menuFile.add(open);
		menuFile.add(save);
		menuFile.add(new JSeparator());
		menuFile.add(pageSetup);
		menuFile.add(print);
		menuFile.add(new JSeparator());
		menuFile.add(exit);
		
		menubar.add(menuFile);
		menubar.add(Box.createGlue());
		JButton button = new JButton(" Customize ");
		button.setBorder(new EtchedBorder(Color.white, Color.black));
		button.setFont(new Font("Terminal",Font.PLAIN,10));
		menubar.add(button);		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1024,768);
		setResizable(false);
		setJMenuBar(menubar);
	}

	public static void main(String[] args) {
		new Sample033007().setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event) {
		
		String action = event.getActionCommand();
		
		if(action.equals("Open")){
			FileDialog openFile = new FileDialog(this,"Open");
			openFile.setVisible(true);
			String path = openFile.getDirectory()+openFile.getFile();
			JOptionPane.showMessageDialog(this,path,"File Path",JOptionPane.PLAIN_MESSAGE);
		}
		if(action.equals("Save")){
			FileDialog openFile = new FileDialog(this,"Open");
			openFile.setVisible(true);
			String path = openFile.getDirectory()+openFile.getFile();
			JOptionPane.showMessageDialog(this,path,"File Path",JOptionPane.PLAIN_MESSAGE);			
		}if(action.equals("Print")){
			PrinterJob printjob = PrinterJob.getPrinterJob();
			boolean isPrint = printjob.printDialog();
		}
		if(action.equals("Page SetUp")){
			PageFormat pageformat = new PageFormat();
			PrinterJob pjob = PrinterJob.getPrinterJob();
			pjob.pageDialog(pageformat);
		}
		if(action.equals("Exit")){
			setVisible(false);
			System.exit(0);
		}
		
	}
}	
